﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Genespy.Common;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.ComponentModel;
using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage; 
// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229

namespace Genespy
{
    /// <summary>
    /// A page that displays an overview of a single group, including a preview of the items
    /// within the group.
    /// </summary>
    public sealed partial class ReportPage : Page, INotifyPropertyChanged
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        private string _note;
        private Report _selectedReport;
        private string _title;
        private int noteNum;
        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        public string note
        {
            get { return _note; }
            set
            {
                if (_note != value)
                {
                    _note = value;
                    OnPropertyChanged("note");
                }
            }
        }
        public Report loaclSelectedReport
        {
            get { return _selectedReport; }
            set
            {
                if (_selectedReport != value)
                {
                    MainPage.selectedReport = value;
                    _selectedReport = MainPage.selectedReport;
                    OnPropertyChanged("loaclSelectedReport");
                }
            }
        }
        public string localTitle {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    OnPropertyChanged("localTitle");
                }
            }
        }
        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }


        public ReportPage()
        {
            noteNum = 1;
            this.InitializeComponent();
            TitleTextBox.Visibility = Visibility.Collapsed; 
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Assign a bindable group to this.DefaultViewModel["Group"]
            // TODO: Assign a collection of bindable items to this.DefaultViewModel["Items"]

            if (MainPage.selectedReport.title != null)
            {
                pageTitle.Text = MainPage.selectedReport.title;
            }
            else
            {
                pageTitle.Text = "This is not the title you are looking for";
            }         

            foreach (Note note in MainPage.selectedReport.notes)
            {
                notesContainer.Children.Add(noteBuilder(note));
            }
        }

        #region NavigationHelper registration
        async private void setImages()
        {
            foreach (MediaObject picture in MainPage.selectedReport.pictures)
            {

                BitmapImage image = new BitmapImage();

                StorageFile tempStorage = await StorageFile.GetFileFromApplicationUriAsync(picture.mediaStorageUri);

                var stream = await tempStorage.OpenAsync(FileAccessMode.Read);
                image.SetSource(stream);

                Image img = new Image();
                img.Source = image;
                pictureBox.Items.Add(img);
                pictureBox.Tapped += on_click_move_to_picture_display_page;
            }

        }
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        private void on_click_move_to_video_display_page(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(VideoDisplayPage));
            }
        }

        private void on_record_video_click(object sender, RoutedEventArgs e)
        {

        }

        private void on_record_audio_click(object sender, RoutedEventArgs e)
        {

        }

        private void on_capture_picture_click(object sender, RoutedEventArgs e)
        {

        }

        private void on_click_move_to_picture_display_page(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(PictureDisplayPage));
            }
        }

        private void on_add_note(object sender, RoutedEventArgs e)
        {
            Note tempNote = new Note(note);
            MainPage.selectedReport.notes.Add(tempNote);
            OnPropertyChanged("loaclSelectedReport");
        }

        private void on_Edit_Title(object sender, RoutedEventArgs e)
        {
            TitleTextBox.Visibility = Visibility.Visible;
            TitleTextBlock.Visibility = Visibility.Collapsed;
            acceptButton.Visibility = Visibility.Visible;
            closeButton.Visibility = Visibility.Visible;
            editButton.Visibility = Visibility.Collapsed; 
        }

        private void on_Accept_TitleChanged(object sender, RoutedEventArgs e)
        {
            TitleTextBox.Visibility = Visibility.Collapsed;
            TitleTextBlock.Visibility = Visibility.Visible;
            acceptButton.Visibility = Visibility.Collapsed;
            closeButton.Visibility = Visibility.Collapsed;
            editButton.Visibility = Visibility.Visible;
            MainPage.selectedReport.title = TitleTextBox.Text;
        }

        private void on_stop_Edit(object sender, RoutedEventArgs e)
        {
            TitleTextBox.Visibility = Visibility.Collapsed;
            TitleTextBlock.Visibility = Visibility.Visible;
            acceptButton.Visibility = Visibility.Collapsed;
            closeButton.Visibility = Visibility.Collapsed;
            editButton.Visibility = Visibility.Visible;
        }
        private void on_click_move_to_audio_display_page(object sender, RoutedEventArgs e)
        {
            if (this.Frame != null)
            {
                this.Frame.Navigate(typeof(AudioDisplayPage));
            }
        }

        #endregion

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void TitleTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            pageTitle.Text = TitleTextBox.Text;
        }

        private void on_add_note(object sender, TappedRoutedEventArgs e)
        {
            Note newNote = new Note(notesTextBox.Text);
            newNote.date = DateTime.UtcNow;
            MainPage.selectedReport.notes.Add(newNote);

            notesContainer.Children.Add(noteBuilder(newNote));
        }

        private StackPanel noteBuilder(Note note)
        {     
            TextBlock uiNote = new TextBlock();
            StackPanel noteContainer = new StackPanel();
            noteContainer.Orientation = Orientation.Horizontal;

            TextBlock noteTitle = new TextBlock();
            noteTitle.Text = "Note " + noteNum++ + " - " + note.date + "===>  ";
            uiNote.Text = note.note;

            noteContainer.Children.Add(noteTitle);
            noteContainer.Children.Add(uiNote);
            return noteContainer;
        }
    }
}
